<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

include ROOT.'php/lib/offres_inc.php';
if(isset($_SESSION['commande_dom'])) {
	if(isset($_SESSION['u_nic'])) {
		if(isset($_POST['submit'])) {
			$error = 0;
			$code = preg_replace('#([^a-zA-Z0-9])+#i', '', $_POST['code']);
			if($_SESSION['commande_extension'] == "fr") {
				$naissance = intval($_POST['naissance_day'])."-".intval($_POST['naissance_mon'])."-".intval($_POST['naissance_yea']);
				$naissance_pays = intval($_POST['naissance_pays']);
				$naissance_ville = preg_replace('#([^A-Z])+#i', ' ', strtoupper($_POST['naissance_ville']));
				$naissance_cp = intval($_POST['naissance_cp']);
				if($naissance == "" OR $naissance_pays == "" OR $naissance_cp == "" OR $naissance_ville == "" OR $_POST['mf'] != 1)  {
					$error = 1;
				}
			}
			if($_POST['cgv'] != 1 OR $_POST['in'] != 1) {
				$error = 1;
			}
			
				if(isset($_SESSION['code_mem'])) {
				$verif_code = query("SELECT * FROM commandes_codes_promos WHERE code = '".$_SESSION['code_mem']."' AND u_nic = '".$_SESSION['u_nic']."' AND 	valable_type= 1 AND valable_parametre = 1 AND valable_current = 0 AND type_commande = 1");
				if(mysql_num_rows($verif_code) == 1) {
					if($error != 1) {
						$verif_code = mysql_fetch_array($verif_code);
						query("INSERT INTO commandes_data(prix, prix_total, type_produit, type_commande, domaine_or_user_linux, commande_etat, u_nic, naissance, naissance_pays, naissance_ville, naissance_cp)
								VALUES(0, 0, 1, 1, '".$_SESSION['commande_dom']."', 0, '".$_SESSION['u_nic']."', '".$naissance."', '".$naissance_pays."', '".$naissance_ville ."', '".$naissance_cp."')");
						query("DELETE FROM commandes_codes_promos WHERE code = '".$_SESSION['code_mem']."'");
						query("UPDATE comptes_heberg SET dom_inclus = 0 WHERE id = '".$verif_code['valable_produit']."'");
						unset($_SESSION['commande_dom']);
						unset($_SESSION['commande_extension']);
						unset($_SESSION['commande_action']);
						unset($_SESSION['code_mem']);
						$error = 2;
					}
				}
				else {
					$error = 3;
				}			
			}
			else {
				$infos = mysql_fetch_array(query("SELECT u_portemonnaie, u_tel, reg_nic FROM users WHERE u_nic = '".$_SESSION['u_nic']."'"));
				if(($infos['reg_nic'] != "") && ($_SESSION['commande_extension'] == "fr")) {
					$infos['reg_update'] = 1;
				}
				else {
					$infos['reg_update'] = 0;
				}
				
				if(($infos['u_tel'] != "") AND (preg_match('`^\+[0-9]{1,3}\.[0-9]{4,12}$`',$infos['u_tel'])) AND (!preg_match('`^\+33\.6[0-9]{8}$`',$infos['u_tel']))) {
					$prix = query("SELECT prix FROM offres WHERE nom = '".$_SESSION['commande_extension']."' AND id_registrar = '".$bzh_config['default_registrar']."'");
					if(mysql_num_rows($prix) == 1) {
						$prix = mysql_fetch_array($prix);
						if($prix['prix'] <= $infos['u_portemonnaie']){
							query("UPDATE users SET u_portemonnaie = u_portemonnaie - ".$prix['prix'].", reg_update = '".$infos['reg_update']."' WHERE u_nic = '".$_SESSION['u_nic']."'");
							query("INSERT INTO commandes_data(prix, prix_total, type_produit, type_commande, domaine_or_user_linux, commande_etat, u_nic, naissance, naissance_pays, naissance_ville, naissance_cp, affiliation)
									VALUES('".$prix['prix']."', '".$prix['prix']."', 1, 1, '".$_SESSION['commande_dom']."', 0, '".$_SESSION['u_nic']."', '".$naissance."', '".$naissance_pays."', '".$naissance_ville ."', '".$naissance_cp."', '".intval($_COOKIE['ref_partner'])."')");
							unset($_SESSION['commande_dom']);
							unset($_SESSION['commande_extension']);
							unset($_SESSION['commande_action']);
							$error = 2;
						}
						else {
							$error = 4;					
						}					
					}
				}
				else {
					$error = 5;
				}
			}
			$tpl->assign(array(
				"error" => $error,
				"ext" => $_SESSION['commande_extension'],
			));
			$title = "Commandez votre nom de domaine";
			$tplinc = "Offres/offres-domaine-commande-6";
			$parse = 1;
		}
		else {
			$parse = 1;
			$infos = mysql_fetch_array(query("SELECT 
											u_nom,
											u_prenom,
											u_email,
											u_adresse,
											u_cp,
											u_ville,
											u_pays,
											u_tel,
											u_email,
											u_portemonnaie,
											UNIX_TIMESTAMP(u_naissance)
											FROM users WHERE u_nic = '".$_SESSION['u_nic']."'"));
			$prix_dom = mysql_fetch_array(query("SELECT prix FROM offres WHERE nom = '".$_SESSION['commande_extension']."' AND id_registrar = '".$bzh_config['default_registrar']."'"));
			if(($prix_dom['prix'] <= $infos['u_portemonnaie'])OR(isset($_SESSION['code_mem']))) {
				$tpl->assign(array(
					'u_nom' => $infos['u_nom'],
					'u_prenom' => $infos['u_prenom'],
					'u_adresse' => $infos['u_adresse'],
					'u_cp' => $infos['u_cp'],
					'u_ville' => $infos['u_ville'],
					'u_adresse' => $infos['u_adresse'],
					'u_pays' => $infos['u_pays'],
					'u_tel' => $infos['u_tel'],
					'u_email' => $infos['u_email'],
					'u_naissance_yea' => date('Y', $infos['UNIX_TIMESTAMP(u_naissance)']),
					'u_naissance_mon' => date('m', $infos['UNIX_TIMESTAMP(u_naissance)']),
					'u_naissance_day' => date('d', $infos['UNIX_TIMESTAMP(u_naissance)']),
					'dom_prix' => $prix_dom['prix']
				));
				$title = "Informations du contact Domaine";
				$tplinc = "Offres/offres-domaine-commande-4";
				if($_SESSION['commande_extension'] == "fr") {
					include ROOT.'php/lib/registrars/online.inc.php';
					$_domgest = new bh_doms_gest();
					$_liste_pays = $_domgest->codes_pays();
					$_domgest->deconnect();
					$_domgest->deconnect();
					$tpl->assign(array(
						'extention' => 'fr',
						'code_pays' => $_liste_pays['retfields'],
						
					));
				}
				if(isset($_SESSION['code_mem'])) {
					$tpl->assign(array(
						'code_mem' => $_SESSION['code_mem'],
					));				
				}
			}
			else {
				$title = "Créditez tout d'abord votre porte monnaie";
				$tplinc = "Offres/offres-domaine-commande-5";
				$tpl->assign(array(
					'dom_prix' => $prix_dom['prix'],
					'p_m' => $infos['u_portemonnaie']
				));
			}
		}
	}
	else {
		$title = "Commander votre nom de domaine";
		$tplinc = "Offres/offres-domaine-commande-3";
		$parse = 1;
	}
}
?>
